package com.recsmile.sgs.ui.components;

import com.recsmile.sgs.entity.GamePanel;
import com.recsmile.sgs.entity.cards.equips.Equipment;
import com.recsmile.sgs.listener.EquipmentListener;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static com.recsmile.sgs.entity.cards.equips.Equipment.EquipmentType.*;

/**
 * Created by Administrator on 2016/8/26.
 */
public class EquipmentRackGui extends JPanel implements EquipmentListener{
    private static final long serialVersionUID = 8319619360735971266L;

    public static final int WIDTH = CardRackGui.HEIGHT;
    public static final int HEIGHT = WIDTH;

    private EquipmentGui weapon;
    private EquipmentGui shield;
    private EquipmentGui horsePlus;
    private EquipmentGui horseMinus;

    public EquipmentRackGui(final GamePanel panel) {
        init();
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.getCurrentOperation().onEquipmentClicked((EquipmentGui) e.getSource());
            }
        };
        weapon.addActionListener(listener);
        shield.addActionListener(listener);
        horsePlus.addActionListener(listener);
        horseMinus.addActionListener(listener);
    }

    public EquipmentRackGui() {
        init();
    }

    private void init() {
        setSize(WIDTH, HEIGHT);
        setLayout(null);
        setLocation(0, GamePanelGui.HEIGHT - HEIGHT);
        weapon = new EquipmentGui(0);
        shield = new EquipmentGui(EquipmentGui.HEIGHT);
        horsePlus = new EquipmentGui(2 * EquipmentGui.HEIGHT);
        horseMinus = new EquipmentGui(3 * EquipmentGui.HEIGHT);
        add(weapon);
        add(shield);
        add(horsePlus);
        add(horseMinus);
    }

    @Override
    public void onEquipped(Equipment equipment) {
        switch (equipment.getEquipmentType()) {
            case WEAPON:
                weapon.setEquipment(equipment);
                break;
            case SHIELD:
                shield.setEquipment(equipment);
                break;
            case HORSEPLUS:
                horsePlus.setEquipment(equipment);
                break;
            case HORSEMINUS:
                horseMinus.setEquipment(equipment);
                break;
            default:
                System.err.println("EquipmentRack: Unidentified Error");
        }
    }

    @Override
    public void onUnequipped(Equipment.EquipmentType type) {
        switch (type) {
            case WEAPON:
                weapon.setEquipment(null);
                break;
            case SHIELD:
                shield.setEquipment(null);
                break;
            case HORSEPLUS:
                horsePlus.setEquipment(null);
                break;
            case HORSEMINUS:
                horseMinus.setEquipment(null);
                break;
            default:
                System.err.println("EquipmentRack: Unidentified Error");
        }
    }
}
